package com.ruoyi.web.controller.module.market;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.market.domain.ConfigProgram;
import com.ruoyi.market.service.IConfigProgramService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 店铺程序配置信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/market/configProgram")
public class ConfigProgramController extends BaseController
{
    @Autowired
    private IConfigProgramService configProgramService;

    /**
     * 获取店铺程序配置列表
     */
    @PostMapping("/configProgramList")
    public TableDataInfo list(@RequestBody ConfigProgram configProgram)
    {
        startPageByPost();
        List<ConfigProgram> list = configProgramService.selectConfigProgramList(configProgram);
        return getDataTable(list);
    }

    /**
     * 根据店铺程序配置编号获取详细信息
     */
    @PostMapping("/getConfigProgram")
    public AjaxResult getConfigProgram(@RequestBody Map<String, Object> params)
    {
        String shopId = params.get("shopId").toString(); // 店铺id
        if (shopId == null || shopId.equals("")) {
            return error("店铺程序配置失败，参数错误");
        }
        AjaxResult ajax = AjaxResult.success();
        ConfigProgram sysConfigProgram = configProgramService.selectConfigProgramByShopId(shopId);
        ajax.put(AjaxResult.DATA_TAG, sysConfigProgram);
        return ajax;
    }

    /**
     * 新增店铺程序配置
     */
    @PreAuthorize("@ss.hasPermi('market:configProgram:add')")
    @Log(title = "店铺程序配置管理 - 新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ConfigProgram configProgram)
    {
        configProgram.setCreateBy(getUsername());
        return toAjax(configProgramService.insertConfigProgram(configProgram));
    }

    /**
     * 修改店铺程序配置
     */
    @PreAuthorize("@ss.hasPermi('market:configProgram:edit')")
    @Log(title = "店铺程序配置管理 - 修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ConfigProgram configProgram)
    {
        configProgram.setUpdateBy(getUsername());
        return toAjax(configProgramService.updateConfigProgram(configProgram));
    }

    /**
     * 保存店铺程序配置
     */
    @PostMapping("/save")
    public AjaxResult save(@Validated @RequestBody ConfigProgram configProgram)
    {
        if(StringUtils.isNotNull(configProgram) && StringUtils.isNotNull(configProgram.getShopId())){
            ConfigProgram cb = configProgramService.selectConfigProgramByShopId(configProgram.getShopId());
            if(StringUtils.isNotNull(cb)){
                configProgramService.updateConfigProgram(configProgram);
            }else{
                configProgramService.insertConfigProgram(configProgram);
            }
        }else {
            return error("保存店铺程序配置失败，参数错误");
        }
        configProgram.setCreateBy(getUsername());
        configProgram.setUpdateBy(getUsername());
        return toAjax(configProgramService.updateConfigProgram(configProgram));
    }

    /**
     * 删除店铺程序配置
     */
    @PreAuthorize("@ss.hasPermi('market:configProgram:remove')")
    @Log(title = "店铺程序配置管理 - 删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configProgramIds}")
    public AjaxResult remove(@PathVariable Long[] configProgramIds)
    {
        return toAjax(configProgramService.deleteConfigProgramByIds(configProgramIds));
    }
}
